#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

//This Unit Test Will Test the updateCoins() Function.
#define MAX_CARD treasure_map

//initialize game struct to array of 0's
void clearGameState(struct gameState *G) {
	int i;
	for (i=0; i<sizeof(struct gameState); i++) {
		((char *)G)[i] = 0;
	}
}

//simple implementation of cards that return values so we can compare to updateCoins()
int getCardValue(int card) {
	if (card == gold) {
		return 3;
	}
	
	if (card == silver) {
		return 2;
	}
	
	if (card == copper) {
		return 1;
	}
	return 0;
}

int main() {	

	int i, j;
	int handCount, bonus, currentPlayer;	//relevant to updateCoins in dominion.c
	
	int seed = 1003;		//FEEL FREE TO CHANGE
	int num_tests = 100;		//FEEL FREE TO CHANGE
	struct gameState G;
	
	int theoreticalValue;
	int errors = 0;
	
	//initialize random number using rngs.c library
	SelectStream(1);
	PutSeed((long)seed);
	
	/*run test, using random player numbers, hands, game states*/
	printf("Now Testing unittest 1: updateCoins() Function:\n");
	for (i=0; i < num_tests; i++) {
		
		theoreticalValue = 0;
		clearGameState(&G);				//initialize game state to all 0's
		currentPlayer = (int)(Random()*3)+1;		//random player number from 1-4
		handCount = (int)(Random()*5);			//random size of the hand in question from 1-5 (use 6?)
		bonus = (int)(Random()*5);			//random bonus
		
		G.handCount[currentPlayer] = handCount;
		
		//Get Random Cards for the random player with their random number of cards
		for (j=0; j < handCount; j++) {
			G.hand[currentPlayer][j] = (int)(Random() * MAX_CARD);
			theoreticalValue += getCardValue(G.hand[currentPlayer][j]);
		}
		
		//add the bonus
		theoreticalValue += bonus;
		
		//Actually Call The Function
		updateCoins(currentPlayer, &G, bonus);
		
		//Compare theoretical value to actual Coin value from Game State
		if (theoreticalValue != G.coins) {
			printf("Error: Theoretical Value was %d, but Game State has value %d\n", theoreticalValue, G.coins);
			errors++;
		}
	
	}
	
	printf("Out of %d tests, %d returned error.\n\n", num_tests, errors);
	
	return 0;
}

